package com.thinkfit.jy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.ShowSpeedViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;


import java.util.HashMap;

import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videocontroller.component.VodControlView;
import xyz.doikki.videoplayer.player.BaseVideoView;
import xyz.doikki.videoplayer.player.VideoView;

public class WarmUpVideoActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View{



    ConstraintLayout clMain;
    VideoView videoView;
    ImageView ivBack;
    private StandardVideoController mController;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_warm_up_video;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        clMain = findViewById(R.id.cl_main);
        videoView = findViewById(R.id.videoView);
        ivBack = findViewById(R.id.iv_back);
        setViewsOnClickListener(R.id.iv_back);

        showVideo("https://fitlog-upload.oss-accelerate.aliyuncs.com/2024712/Progressive_Running_Training.mp4");

    }

    private void showVideo(String url) {
//        getNiceVideoPlayer().setPlayerType(NiceVideoPlayer.TYPE_NATIVE);//这个没办法设置播放速度
//        getNiceVideoPlayer().setPlayerType(NiceVideoPlayer.TYPE_IJK);

        mController = new StandardVideoController(this);
//        mController.setTitle("");

        videoView.setVideoController(mController);
        VodControlView vodControlView = new VodControlView(this);//点播控制条
        //是否显示底部进度条。默认显示
//                vodControlView.showBottomProgress(false);
        mController.addControlComponent(vodControlView);
        //竖屏也开启手势操作，默认关闭
        mController.setEnableInNormal(true);
        //滑动调节亮度，音量，进度，默认开启
        mController.setGestureEnabled(true);

        if (!TextUtils.isEmpty(url)) {
            videoView.setUrl(url, null);
        }
        videoView.addOnStateChangeListener(new BaseVideoView.OnStateChangeListener() {
            @Override
            public void onPlayerStateChanged(int playerState) {

            }

            @Override
            public void onPlayStateChanged(int playState) {
                switch (playState) {
                    case VideoView.STATE_IDLE:
                        break;
                    case VideoView.STATE_PREPARING:
                        //视频准备中
                        break;
                    case VideoView.STATE_PREPARED:
                        //视频准备好了
                        break;
                    case VideoView.STATE_PLAYING:
                        //视频播放中
                        //需在此时获取视频宽高
//                        int[] videoSize = mVideoView.getVideoSize();
//                        L.d("视频宽：" + videoSize[0]);
//                        L.d("视频高：" + videoSize[1]);
                        break;
                    case VideoView.STATE_PAUSED:
                        //视频暂停
                        break;
                    case VideoView.STATE_BUFFERING:
                        //缓存中
                        break;
                    case VideoView.STATE_BUFFERED:
                        break;
                    case VideoView.STATE_PLAYBACK_COMPLETED:
                        //视频播放完成
                        break;
                    case VideoView.STATE_ERROR:
                        //视频错误
                        break;
                }
            }
        });
        videoView.start();
//        BaseUtils.StaticParams.loadImage(this, 0, mVideo.getCover(), findViewById(R.id.iv));
//        mController.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        ShowSpeedViewUtils.getInstance().goneLayout();
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.pause();
        videoView.release();

    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.BLUETOOTH_SUCCESS)) {
            finish();
        }if (map.containsKey(EventBusEvent.BLUETOOTH_CHANGE)) {
//            showBottomUI();
            boolean b = (boolean) map.get(EventBusEvent.BLUETOOTH_CHANGE);
            if(b){
                setImage(R.id.iv_bluetooth,R.drawable.img_main_bluetooth);
            }else {
                setImage(R.id.iv_bluetooth, R.drawable.img_main_bluetooth_back);
            }
        } else {
            super.onReceive(map);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
